package site.marqstree.smartbutler.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import site.marqstree.smartbutler.MainActivity;
import site.marqstree.smartbutler.R;
import site.marqstree.smartbutler.entity.User;
import site.marqstree.smartbutler.util.ShareUtils;
import site.marqstree.smartbutler.view.CustomDialog;

/*
 * 项目名: SmartButler
 * 包名: site.marqstree.smartbutler.ui
 * 文件名: LoginActivity
 * 创建者: marqstree
 * 创建时间: 2019/12/31 14:28
 * 描述: 登陆页面
 */
public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.profile_image)
    ImageView profileImage;
    @BindView(R.id.etName)
    EditText etName;
    @BindView(R.id.etPassword)
    EditText etPassword;
    @BindView(R.id.cbKeepPassword)
    CheckBox keepPassword;
    @BindView(R.id.btnLogin)
    Button btnLogin;
    @BindView(R.id.btnRegistered)
    Button btnRegistered;
    @BindView(R.id.tvForget)
    TextView tvForget;
    //自定义进度条对话框
    private CustomDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        //恢复用户名/密码
        boolean isKeep = ShareUtils.getBoolean(this, "keeppass", false);
        keepPassword.setChecked(isKeep);
        if (isKeep) {
            String name = ShareUtils.getString(this, "name", "");
            String password = ShareUtils.getString(this, "password", "");
            etName.setText(name);
            etPassword.setText(password);
        }

        //初始化进度条对话框
        dialog = new CustomDialog(this, 100, 100,
                R.layout.dialog_loding,
                R.style.Theme_dialog,
                Gravity.CENTER,
                R.style.pop_anim_style);
        dialog.setCancelable(false);
    }

    //注册按钮事件
    @OnClick(R.id.btnRegistered)
    public void onBtnRegisteredClicked(View v) {
        switch (v.getId()) {
            case R.id.btnRegistered:
                startActivity(new Intent(this, RegisteredActivity.class));
                break;
        }
    }

    //登陆按钮事件
    @OnClick(R.id.btnLogin)
    public void onBtnLoginClicked(View v) {
        switch (v.getId()) {
            case R.id.btnLogin:
                //1.获取输入框的值
                String name = etName.getText().toString().trim();
                String password = etPassword.getText().toString().trim();

                //2.判断是否为空
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                //登录
                //显示进度条对话框
                dialog.show();
                final User user = new User();
                user.setUsername(name);
                user.setPassword(password);
                user.login(new SaveListener<User>() {
                    @Override
                    public void done(User myUser, BmobException e) {
                        //结束进度条对话框
                        dialog.dismiss();
                        //判断结果
                        if (e == null) {
                            //判断邮箱是否验证
                            if (user.getEmailVerified()) {
                                //跳转
                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                finish();
                            } else {
                                Toast.makeText(LoginActivity.this, "请前往邮箱验证", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(LoginActivity.this, "登录失败：" + e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;
        }
    }

    //忘记密码按钮点击事件
    @OnClick(R.id.tvForget)
    public void onTvForgetClicked() {
        startActivity(new Intent(this, ForgetPasswordActivity.class));
    }

    //假设我现在输入用户名和密码，但是我不点击登录，而是直接退出了
    //此时需要存储用户名和密码
    @Override
    protected void onDestroy() {
        super.onDestroy();

        //保存状态
        ShareUtils.putBoolean(this, "keeppass", keepPassword.isChecked());

        //是否记住密码
        if (keepPassword.isChecked()) {
            //记住用户名和密码
            ShareUtils.putString(this, "name", etName.getText().toString().trim());
            ShareUtils.putString(this, "password", etPassword.getText().toString().trim());
        } else {
            ShareUtils.deleShare(this, "name");
            ShareUtils.deleShare(this, "password");
        }
    }

}
